home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / contrib / dvx / inc / x11 / poly.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-15  |  10.1 KB  |  274 lines

  1. /* $XConsortium: poly.h,v 1.2 88/09/06 16:11:29 jim Exp $ */
  2. /************************************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ************************************************************************/
  25. #include <sys\param.h>
  26.  
  27. /*
  28.  *     This file contains a few macros to help track
  29.  *     the edge of a filled object.  The object is assumed
  30.  *     to be filled in scanline order, and thus the
  31.  *     algorithm used is an extension of Bresenham's line
  32.  *     drawing algorithm which assumes that y is always the
  33.  *     major axis.
  34.  *     Since these pieces of code are the same for any filled shape,
  35.  *     it is more convenient to gather the library in one
  36.  *     place, but since these pieces of code are also in
  37.  *     the inner loops of output primitives, procedure call
  38.  *     overhead is out of the question.
  39.  *     See the author for a derivation if needed.
  40.  */
  41.  
  42.  
  43. /*
  44.  *  In scan converting polygons, we want to choose those pixels
  45.  *  which are inside the polygon.  Thus, we add .5 to the starting
  46.  *  x coordinate for both left and right edges.  Now we choose the
  47.  *  first pixel which is inside the pgon for the left edge and the
  48.  *  first pixel which is outside the pgon for the right edge.
  49.  *  Draw the left pixel, but not the right.
  50.  *
  51.  *  How to add .5 to the starting x coordinate:
  52.  *      If the edge is moving to the right, then subtract dy from the
  53.  *  error term from the general form of the algorithm.
  54.  *      If the edge is moving to the left, then add dy to the error term.
  55.  *
  56.  *  The reason for the difference between edges moving to the left
  57.  *  and edges moving to the right is simple:  If an edge is moving
  58.  *  to the right, then we want the algorithm to flip immediately.
  59.  *  If it is moving to the left, then we don't want it to flip until
  60.  *  we traverse an entire pixel.
  61.  */
  62. #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
  63.     int dx;      /* local storage */ \
  64. \
  65.     /* \
  66.      *  if the edge is horizontal, then it is ignored \
  67.      *  and assumed not to be processed.  Otherwise, do this stuff. \
  68.      */ \
  69.     if ((dy) != 0) { \
  70.         xStart = (x1); \
  71.         dx = (x2) - xStart; \
  72.         if (dx < 0) { \
  73.             m = dx / (dy); \
  74.             m1 = m - 1; \
  75.             incr1 = -2 * dx + 2 * (dy) * m1; \
  76.             incr2 = -2 * dx + 2 * (dy) * m; \
  77.             d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
  78.         } else { \
  79.             m = dx / (dy); \
  80.             m1 = m + 1; \
  81.             incr1 = 2 * dx - 2 * (dy) * m1; \
  82.             incr2 = 2 * dx - 2 * (dy) * m; \
  83.             d = -2 * m * (dy) + 2 * dx; \
  84.         } \
  85.     } \
  86. }
  87.  
  88. #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
  89.     if (m1 > 0) { \
  90.         if (d > 0) { \
  91.             minval += m1; \
  92.             d += incr1; \
  93.         } \
  94.         else { \
  95.             minval += m; \
  96.             d += incr2; \
  97.         } \
  98.     } else {\
  99.         if (d >= 0) { \
  100.             minval += m1; \
  101.             d += incr1; \
  102.         } \
  103.         else { \
  104.             minval += m; \
  105.             d += incr2; \
  106.         } \
  107.     } \
  108. }
  109.  
  110.  
  111. /*
  112.  *     This structure contains all of the information needed
  113.  *     to run the bresenham algorithm.
  114.  *     The variables may be hardcoded into the declarations
  115.  *     instead of using this structure to make use of
  116.  *     register declarations.
  117.  */
  118. typedef struct _BRESINFO { /* Tagged. POHC 90/10/08 */
  119.     int minor_axis;    /* minor axis        */
  120.     int d;        /* decision variable */
  121.     int m, m1;        /* slope and slope+1 */
  122.     int incr1, incr2;    /* error increments */
  123. } BRESINFO;
  124.  
  125.  
  126. #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
  127.     BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
  128.                      bres.m, bres.m1, bres.incr1, bres.incr2)
  129.  
  130. #define BRESINCRPGONSTRUCT(bres) \
  131.         BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
  132.  
  133.  
  134.  
  135. /*
  136.  *     These are the data structures needed to scan
  137.  *     convert regions.  Two different scan conversion
  138.  *     methods are available -- the even-odd method, and
  139.  *     the winding number method.
  140.  *     The even-odd rule states that a point is inside
  141.  *     the polygon if a ray drawn from that point in any
  142.  *     direction will pass through an odd number of
  143.  *     path segments.
  144.  *     By the winding number rule, a point is decided
  145.  *     to be inside the polygon if a ray drawn from that
  146.  *     point in any direction passes through a different
  147.  *     number of clockwise and counter-clockwise path
  148.  *     segments.
  149.  *
  150.  *     These data structures are adapted somewhat from
  151.  *     the algorithm in (Foley/Van Dam) for scan converting
  152.  *     polygons.
  153.  *     The basic algorithm is to start at the top (smallest y)
  154.  *     of the polygon, stepping down to the bottom of
  155.  *     the polygon by incrementing the y coordinate.  We
  156.  *     keep a list of edges which the current scanline crosses,
  157.  *     sorted by x.  This list is called the Active Edge Table (AET)
  158.  *     As we change the y-coordinate, we update each entry in 
  159.  *     in the active edge table to reflect the edges new xcoord.
  160.  *     This list must be sorted at each scanline in case
  161.  *     two edges intersect.
  162.  *     We also keep a data structure known as the Edge Table (ET),
  163.  *     which keeps track of all the edges which the current
  164.  *     scanline has not yet reached.  The ET is basically a
  165.  *     list of ScanLineList structures containing a list of
  166.  *     edges which are entered at a given scanline.  There is one
  167.  *     ScanLineList per scanline at which an edge is entered.
  168.  *     When we enter a new edge, we move it from the ET to the AET.
  169.  *
  170.  *     From the AET, we can implement the even-odd rule as in
  171.  *     (Foley/Van Dam).
  172.  *     The winding number rule is a little trickier.  We also
  173.  *     keep the EdgeTableEntries in the AET linked by the
  174.  *     nextWETE (winding EdgeTableEntry) link.  This allows
  175.  *     the edges to be linked just as before for updating
  176.  *     purposes, but only uses the edges linked by the nextWETE
  177.  *     link as edges representing spans of the polygon to
  178.  *     drawn (as with the even-odd rule).
  179.  */
  180.  
  181. /*
  182.  * for the winding number rule
  183.  */
  184. #define CLOCKWISE          1
  185. #define COUNTERCLOCKWISE  -1 
  186.  
  187. typedef struct _EdgeTableEntry {
  188.      int ymax;             /* ycoord at which we exit this edge. */
  189.      BRESINFO bres;        /* Bresenham info to run the edge     */
  190.      struct _EdgeTableEntry *next;       /* next in the list     */
  191.      struct _EdgeTableEntry *back;       /* for insertion sort   */
  192.      struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
  193.      int ClockWise;        /* flag for winding number rule       */
  194. } EdgeTableEntry;
  195.  
  196.  
  197. typedef struct _ScanLineList {
  198.      int scanline;              /* the scanline represented */
  199.      EdgeTableEntry *edgelist;  /* header node              */
  200.      struct _ScanLineList *next;  /* next in the list       */
  201. } ScanLineList;
  202.  
  203.  
  204. typedef struct _EdgeTable { /* Tagged. POHC 90/10/08 */
  205.      int ymax;                 /* ymax for the polygon     */
  206.      int ymin;                 /* ymin for the polygon     */
  207.      ScanLineList scanlines;   /* header node              */
  208. } EdgeTable;
  209.  
  210.  
  211. /*
  212.  * Here is a struct to help with storage allocation
  213.  * so we can allocate a big chunk at a time, and then take
  214.  * pieces from this heap when we need to.
  215.  */
  216. #define SLLSPERBLOCK 25
  217.  
  218. typedef struct _ScanLineListBlock {
  219.      ScanLineList SLLs[SLLSPERBLOCK];
  220.      struct _ScanLineListBlock *next;
  221. } ScanLineListBlock;
  222.  
  223.  
  224.  
  225. /*
  226.  *
  227.  *     a few macros for the inner loops of the fill code where
  228.  *     performance considerations don't allow a procedure call.
  229.  *
  230.  *     Evaluate the given edge at the given scanline.
  231.  *     If the edge has expired, then we leave it and fix up
  232.  *     the active edge table; otherwise, we increment the
  233.  *     x value to be ready for the next scanline.
  234.  *     The winding number rule is in effect, so we must notify
  235.  *     the caller when the edge has been removed so he
  236.  *     can reorder the Winding Active Edge Table.
  237.  */
  238. #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
  239.    if (pAET->ymax == y) {          /* leaving this edge */ \
  240.       pPrevAET->next = pAET->next; \
  241.       pAET = pPrevAET->next; \
  242.       fixWAET = 1; \
  243.       if (pAET) \
  244.          pAET->back = pPrevAET; \
  245.    } \
  246.    else { \
  247.       BRESINCRPGONSTRUCT(pAET->bres); \
  248.       pPrevAET = pAET; \
  249.       pAET = pAET->next; \
  250.    } \
  251. }
  252.  
  253.  
  254. /*
  255.  *     Evaluate the given edge at the given scanline.
  256.  *     If the edge has expired, then we leave it and fix up
  257.  *     the active edge table; otherwise, we increment the
  258.  *     x value to be ready for the next scanline.
  259.  *     The even-odd rule is in effect.
  260.  */
  261. #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
  262.    if (pAET->ymax == y) {          /* leaving this edge */ \
  263.       pPrevAET->next = pAET->next; \
  264.       pAET = pPrevAET->next; \
  265.       if (pAET) \
  266.          pAET->back = pPrevAET; \
  267.    } \
  268.    else { \
  269.       BRESINCRPGONSTRUCT(pAET->bres); \
  270.       pPrevAET = pAET; \
  271.       pAET = pAET->next; \
  272.    } \
  273. }
  274.